<template><div><p>本文档最新版为 <a href="https://learnku.com/docs/laravel/10.x" target="_blank" rel="noopener noreferrer">10.x</a>，旧版本可能放弃维护，推荐阅读最新版！</p>
<h2 id="laravel-的事件广播系统" tabindex="-1"><a class="header-anchor" href="#laravel-的事件广播系统"><span>Laravel 的事件广播系统</span></a></h2>
<ul>
<li><a href="#introduction">简介</a>
<ul>
<li><a href="#configuration">配置</a></li>
<li><a href="#driver-prerequisites">对驱动器的要求</a></li>
</ul>
</li>
<li><a href="#concept-overview">概念综述</a>
<ul>
<li><a href="#using-example-application">使用示例程序</a></li>
</ul>
</li>
<li><a href="#defining-broadcast-events">定义广播事件</a>
<ul>
<li><a href="#broadcast-name">广播名称</a></li>
<li><a href="#broadcast-data">广播数据</a></li>
<li><a href="#broadcast-queue">广播队列</a></li>
<li><a href="#broadcast-conditions">广播条件</a></li>
</ul>
</li>
<li><a href="#authorizing-channels">频道授权</a>
<ul>
<li><a href="#defining-authorization-routes">定义授权路由</a></li>
<li><a href="#defining-authorization-callbacks">定义授权回调</a></li>
</ul>
</li>
<li><a href="#broadcasting-events">对事件进行广播</a>
<ul>
<li><a href="#only-to-others">只广播给他人</a></li>
</ul>
</li>
<li><a href="#receiving-broadcasts">接受广播</a>
<ul>
<li><a href="#installing-laravel-echo">安装 Laravel Echo</a></li>
<li><a href="#listening-for-events">对事件进行监听</a></li>
<li><a href="#leaving-a-channel">退出频道</a></li>
<li><a href="#namespaces">命名空间</a></li>
</ul>
</li>
<li><a href="#presence-channels">Presence 频道</a>
<ul>
<li><a href="#authorizing-presence-channels">授权 Presence 频道</a></li>
<li><a href="#joining-presence-channels">加入 Presence 频道</a></li>
<li><a href="#broadcasting-to-presence-channels">广播到 Presence 频道</a></li>
</ul>
</li>
<li><a href="#client-events">客户端事件</a></li>
<li><a href="#notifications">消息通知</a></li>
</ul>
<h2 id="简介" tabindex="-1"><a class="header-anchor" href="#简介"><span>简介</span></a></h2>
<p>在现代的 web 应用程序中，WebSockets 被用来实现需要实时、即时更新的接口。当服务器上的数据被更新后，更新信息将通过 WebSocket 连接发送到客户端等待处理。相比于不停地轮询应用程序，WebSocket 是一种更加可靠和高效的选择。</p>
<p>为了帮助你建立这类应用, Laravel 将通过 WebSocket 连接来使「广播」<a href="https://learnku.com/docs/laravel/5.5/events" target="_blank" rel="noopener noreferrer">事件</a> 变得更加轻松。广播事件允许你在服务端代码和客户端 JavaScript 应用之间共享相同的事件名。</p>
<blockquote>
<p>{tip} 在深入了解事件广播之前，请确认你已阅读所有关于 Laravel <a href="https://learnku.com/docs/laravel/5.5/events" target="_blank" rel="noopener noreferrer">事件和侦听器</a> 的文档。</p>
</blockquote>
<h3 id="配置" tabindex="-1"><a class="header-anchor" href="#配置"><span>配置</span></a></h3>
<p>所有关于事件广播的配置都被保存在 <code v-pre>config/broadcasting.php</code> 文件中。 Laravel 自带了几个广播驱动器：<a href="https://pusher.com/" target="_blank" rel="noopener noreferrer">Pusher</a>, <a href="https://learnku.com/docs/laravel/5.5/redis" target="_blank" rel="noopener noreferrer">Redis</a>, 和一个用于本地开发与调试的 <code v-pre>log</code> 驱动器。另外，还有一个 <code v-pre>null</code> 驱动器可以让你完全关闭广播功能。每一个驱动的示例配置都可以在 <code v-pre>config/broadcasting.php</code> 文件中被找到。</p>
<h4 id="广播服务提供者" tabindex="-1"><a class="header-anchor" href="#广播服务提供者"><span>广播服务提供者</span></a></h4>
<p>在对事件进行广播之前，你必须先注册 <code v-pre>App\Providers\BroadcastServiceProvider</code>。对于一个全新安装的 Laravel 应用程序，你只需在 <code v-pre>config/app.php</code> 配置文件的 <code v-pre>providers</code> 数组中取消对该提供者的注释即可。该提供者将允许你注册广播授权路由和回调。</p>
<h4 id="csrf-令牌" tabindex="-1"><a class="header-anchor" href="#csrf-令牌"><span>CSRF 令牌</span></a></h4>
<p><a href="#installing-laravel-echo">Laravel Echo</a> 会需要访问当前会话的 CSRF 令牌。如果可用，Echo 会从 <code v-pre>Laravel.csrfToken</code> JavaScript 对象中获取该令牌。如果你运行了 <code v-pre>make:auth</code> Artisan 命令，该对象会在 <code v-pre>resources/views/layouts/app.blade.php</code> 布局文件中被定义。如果你未使用该布局文件，可以在应用程序的 <code v-pre>head</code> HTML 元素中定义一个 <code v-pre>meta</code> 标签：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token operator">&lt;</span>meta name<span class="token operator">=</span><span class="token string double-quoted-string">"csrf-token"</span> content<span class="token operator">=</span><span class="token string double-quoted-string">"{{ csrf_token() }}"</span><span class="token operator">></span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="对驱动器的要求" tabindex="-1"><a class="header-anchor" href="#对驱动器的要求"><span>对驱动器的要求</span></a></h3>
<h4 id="pusher" tabindex="-1"><a class="header-anchor" href="#pusher"><span>Pusher</span></a></h4>
<p>如果你使用 <a href="https://pusher.com/" target="_blank" rel="noopener noreferrer">Pusher</a> 对事件进行广播，请用 Composer 包管理器来安装 Pusher PHP SDK：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">composer <span class="token keyword">require</span> pusher<span class="token operator">/</span>pusher<span class="token operator">-</span>php<span class="token operator">-</span>server <span class="token string double-quoted-string">"~3.0"</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>然后，你需要在 <code v-pre>config/broadcasting.php</code> 配置文件中填写你的 Pusher 证书。该文件中已经包含了一个 Pusher 示例配置，你只需指定 Pusher key、secret 和 application ID 即可。<code v-pre>config/broadcasting.php</code> 中的 <code v-pre>pusher</code> 配置项同时也允许你指定 Pusher 支持的 <code v-pre>options</code> ，例如 cluster：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'options'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'cluster'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'eu'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'encrypted'</span> <span class="token operator">=></span> <span class="token constant boolean">true</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>当把 Pusher 和 <a href="#installing-laravel-echo">Laravel Echo</a> 一起使用时，你应该在 <code v-pre>resources/assets/js/bootstrap.js</code> 文件中实例化 Echo 对象时指定 <code v-pre>pusher</code> 作为所需要的 broadcaster :</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">import <span class="token keyword">Echo</span> from <span class="token string double-quoted-string">"laravel-echo"</span></span>
<span class="line"></span>
<span class="line">window<span class="token operator">.</span>Pusher <span class="token operator">=</span> <span class="token keyword">require</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'pusher-js'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">window<span class="token operator">.</span><span class="token keyword">Echo</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token keyword">Echo</span><span class="token punctuation">(</span><span class="token punctuation">{</span></span>
<span class="line">    broadcaster<span class="token punctuation">:</span> <span class="token string single-quoted-string">'pusher'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">key</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'your-pusher-key'</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="redis" tabindex="-1"><a class="header-anchor" href="#redis"><span>Redis</span></a></h4>
<p>如果你使用 Redis 广播器，请安装 Predis 库：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">composer <span class="token keyword">require</span> predis<span class="token operator">/</span>predis</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>Redis 广播器会使用 Redis 的「生产者/消费者」特性来广播消息；尽管如此，你仍需将它与 WebSocket 服务器一起使用。WebSocket 服务器会从 Redis 接收消息，然后再将消息广播到你的 WebSocket 频道上去。</p>
<p>当 Redis 广播器发布一个事件时，该事件会被发布到它指定的频道上去，传输的数据是一个采用 JSON 编码的字符串。该字符串包含了事件名、 <code v-pre>data</code> 数据和生成该事件套接字 ID 的用户（如果可用的话）。</p>
<h4 id="socket-io" tabindex="-1"><a class="header-anchor" href="#socket-io"><span>Socket.IO</span></a></h4>
<p>如果你想把 Redis 广播器和 Socket.IO 服务器一起使用，你需要将 Socket.IO JavaScript 客户端库文件包含到应用程序的 <code v-pre>head</code> HTML 元素中。当 Socket.IO 服务启动的时候，它会自动把 Socket.IO JavaScript 客户端库暴露在一个标准的 URL中。例如，如果你的应用和 Socket.IO 服务器运行在同域名下，你可以像下面这样来访问你的 Socket.IO JavaScript 客户端库：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token operator">&lt;</span>script src<span class="token operator">=</span><span class="token string double-quoted-string">"//{{ Request::getHost() }}:6001/socket.io/socket.io.js"</span><span class="token operator">></span><span class="token operator">&lt;</span><span class="token operator">/</span>script<span class="token operator">></span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>接着，你需要在实例化 Echo 时指定 <code v-pre>socket.io</code> 连接器和 <code v-pre>host</code>。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">import <span class="token keyword">Echo</span> from <span class="token string double-quoted-string">"laravel-echo"</span></span>
<span class="line"></span>
<span class="line">window<span class="token operator">.</span><span class="token keyword">Echo</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token keyword">Echo</span><span class="token punctuation">(</span><span class="token punctuation">{</span></span>
<span class="line">    broadcaster<span class="token punctuation">:</span> <span class="token string single-quoted-string">'socket.io'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">host</span><span class="token punctuation">:</span> window<span class="token operator">.</span>location<span class="token operator">.</span>hostname <span class="token operator">+</span> <span class="token string single-quoted-string">':6001'</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>最后，你需要运行一个与 Laravel 兼容的 Socket.IO 服务器。Laravel 官方并没有实现 Socket.IO 服务器；不过，可以选择一个由社区驱动维护的项目 <a href="https://github.com/tlaverdure/laravel-echo-server" target="_blank" rel="noopener noreferrer">tlaverdure/laravel-echo-server</a> ，目前托管在 GitHub。</p>
<h4 id="对队列的要求" tabindex="-1"><a class="header-anchor" href="#对队列的要求"><span>对队列的要求</span></a></h4>
<p>在开始广播事件之前，你还需要配置和运行 <a href="https://learnku.com/docs/laravel/5.5/queues" target="_blank" rel="noopener noreferrer">队列侦听器</a> 。所有的事件广播都是通过队列任务来完成的，因此应用程序的响应时间不会受到明显影响。</p>
<h2 id="概念综述" tabindex="-1"><a class="header-anchor" href="#概念综述"><span>概念综述</span></a></h2>
<p>Laravel 的事件广播允许你使用基于驱动的 WebSockets 将服务端的 Larevel 事件广播到客户端的 JavaScript 应用程序。当前的 Laravel 自带了 <a href="https://pusher.com/" target="_blank" rel="noopener noreferrer">Pusher</a> 和 Redis 驱动。通过使用 <a href="#installing-laravel-echo">Laravel Echo</a> 的 Javascript 包，我们可以很方便地在客户端消费事件。</p>
<p>事件通过「频道」来广播，这些频道可以被指定为公开的或私有的。任何访客都可以订阅一个不需要认证和授权的公开频道；然而，如果想订阅一个私有频道，那么该用户必须通过认证，并获得该频道的授权。</p>
<h3 id="使用示例程序" tabindex="-1"><a class="header-anchor" href="#使用示例程序"><span>使用示例程序</span></a></h3>
<p>让我们先用一个电子商务网站作为例子来概览一下事件广播。我们不会讨论如何配置 <a href="https://pusher.com/" target="_blank" rel="noopener noreferrer">Pusher</a> 或者 <a href="#installing-laravel-echo">Laravel Echo</a> 的细节，因为这些会在本文档的其他章节被详细介绍。</p>
<p>在我们的应用程序中，让我们假设有一个允许用户查看订单配送状态的页面。有一个 <code v-pre>ShippingStatusUpdated</code> 事件会在配送状态更新时被触发：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">event</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">ShippingStatusUpdated</span><span class="token punctuation">(</span><span class="token variable">$update</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="shouldbroadcast-接口" tabindex="-1"><a class="header-anchor" href="#shouldbroadcast-接口"><span><code v-pre>ShouldBroadcast</code> 接口</span></a></h4>
<p>当用户在查看自己的订单时，我们不希望他们必须通过刷新页面才能看到状态更新。我们希望一旦有更新时就主动将更新信息广播到客户端。所以，我们必须让 <code v-pre>ShippingStatusUpdated</code> 事件实现 <code v-pre>ShouldBroadcast</code> 接口。这会让 Laravel 在事件被触发时广播该事件：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Events</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Broadcasting<span class="token punctuation">\</span>Channel</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Queue<span class="token punctuation">\</span>SerializesModels</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Broadcasting<span class="token punctuation">\</span>PrivateChannel</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Broadcasting<span class="token punctuation">\</span>PresenceChannel</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Broadcasting<span class="token punctuation">\</span>InteractsWithSockets</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Contracts<span class="token punctuation">\</span>Broadcasting<span class="token punctuation">\</span>ShouldBroadcast</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">ShippingStatusUpdated</span> <span class="token keyword">implements</span> <span class="token class-name">ShouldBroadcast</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * Information about the shipping status update.</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@var</span> <span class="token class-name"><span class="token keyword">string</span></span></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token variable">$update</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>ShouldBroadcast</code> 接口要求事件实现 <code v-pre>broadcastOn</code> 方法。该方法负责指定事件被广播到哪些频道。在通过 Artisan 命令生成的事件类中，一个空的 <code v-pre>broadcastOn</code> 方法已经被预定义好了，所以我们要做的仅仅是指定频道。我们希望只有订单的创建者能够看到状态更新，所以我们要把该事件广播到与这个订单绑定的私有频道上去：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * Get the channels the event should broadcast on.</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">array</span></span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">broadcastOn</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">PrivateChannel</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'order.'</span><span class="token operator">.</span><span class="token variable">$this</span><span class="token operator">-></span><span class="token property">update</span><span class="token operator">-></span><span class="token property">order_id</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="频道授权" tabindex="-1"><a class="header-anchor" href="#频道授权"><span>频道授权</span></a></h4>
<p>记住，用户只有在被授权后才能监听私有频道。我们可以在 <code v-pre>routes/channels.php</code> 文件中定义频道的授权规则。在本例中，我们需要对试图监听私有 <code v-pre>order.1</code> 频道的所有用户进行验证，确保只有订单的创建者才能进行监听：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Broadcast</span><span class="token operator">::</span><span class="token function">channel</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'order.{orderId}'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$user</span><span class="token punctuation">,</span> <span class="token variable">$orderId</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$user</span><span class="token operator">-></span><span class="token property">id</span> <span class="token operator">===</span> <span class="token class-name static-context">Order</span><span class="token operator">::</span><span class="token function">findOrNew</span><span class="token punctuation">(</span><span class="token variable">$orderId</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token property">user_id</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>channel</code> 方法接收两个参数：频道名称和一个回调函数，该回调通过返回 <code v-pre>true</code> 或者 <code v-pre>false</code> 来表示用户是否被授权监听该频道。</p>
<p>所有的授权回调接收当前被认证的用户作为第一个参数，任何额外的通配符参数作为后续参数。在本例中，我们使用 <code v-pre>{orderId}</code> 占位符来表示频道名称的「ID」部分是通配符。</p>
<h4 id="对事件广播进行监听" tabindex="-1"><a class="header-anchor" href="#对事件广播进行监听"><span>对事件广播进行监听</span></a></h4>
<p>接下来，就只剩下在 JavaScript 应用程序中监听事件了。我们可以使用 Laravel Echo 来实现。首先，使用 <code v-pre>private</code> 方法来订阅私有频道。然后，使用 <code v-pre>listen</code> 方法来监听 <code v-pre>ShippingStatusUpdated</code> 事件。默认情况下，事件的所有公有属性会被包括在广播事件中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">Echo</span><span class="token operator">.</span><span class="token keyword">private</span><span class="token punctuation">(</span><span class="token string backtick-quoted-string">`order.${orderId}`</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">.</span><span class="token function">listen</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'ShippingStatusUpdated'</span><span class="token punctuation">,</span> <span class="token punctuation">(</span>e<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span></span>
<span class="line">        console<span class="token operator">.</span><span class="token function">log</span><span class="token punctuation">(</span>e<span class="token operator">.</span>update<span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="定义广播事件" tabindex="-1"><a class="header-anchor" href="#定义广播事件"><span>定义广播事件</span></a></h2>
<p>要告知 Laravel 一个给定的事件是广播类型，只需在事件类中实现 <code v-pre>Illuminate\Contracts\Broadcasting\ShouldBroadcast</code> 接口即可。该接口已经被导入到所有由框架生成的事件类中，所以你可以很方便地将它添加到你自己的事件中。</p>
<p><code v-pre>ShouldBroadcast</code> 接口要求你实现一个方法：<code v-pre>broadcastOn</code>. <code v-pre>broadcastOn</code> 方法返回一个频道或一个频道数组，事件会被广播到这些频道。频道必须是 <code v-pre>Channel</code>、<code v-pre>PrivateChannel</code> 或 <code v-pre>PresenceChannel</code> 的实例。<code v-pre>Channel</code> 实例表示任何用户都可以订阅的公开频道，而 <code v-pre>PrivateChannels</code> 和 <code v-pre>PresenceChannels</code> 则表示需要 <a href="#authorizing-channels">频道授权</a> 的私有频道：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Events</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Broadcasting<span class="token punctuation">\</span>Channel</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Queue<span class="token punctuation">\</span>SerializesModels</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Broadcasting<span class="token punctuation">\</span>PrivateChannel</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Broadcasting<span class="token punctuation">\</span>PresenceChannel</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Broadcasting<span class="token punctuation">\</span>InteractsWithSockets</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Contracts<span class="token punctuation">\</span>Broadcasting<span class="token punctuation">\</span>ShouldBroadcast</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">ServerCreated</span> <span class="token keyword">implements</span> <span class="token class-name">ShouldBroadcast</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">use</span> <span class="token package">SerializesModels</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">public</span> <span class="token variable">$user</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 创建一个新的事件实例</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">void</span></span></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">__construct</span><span class="token punctuation">(</span><span class="token class-name type-declaration">User</span> <span class="token variable">$user</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">user</span> <span class="token operator">=</span> <span class="token variable">$user</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 指定事件在哪些频道上进行广播</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@return</span> <span class="token class-name">Channel<span class="token punctuation">|</span><span class="token keyword">array</span></span></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">broadcastOn</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">PrivateChannel</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'user.'</span><span class="token operator">.</span><span class="token variable">$this</span><span class="token operator">-></span><span class="token property">user</span><span class="token operator">-></span><span class="token property">id</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>然后，你只需要像你平时那样 <a href="https://learnku.com/docs/laravel/5.5/events" target="_blank" rel="noopener noreferrer">触发事件</a> 。一旦事件被触发，一个 <a href="https://learnku.com/docs/laravel/5.5/queues" target="_blank" rel="noopener noreferrer">队列任务</a> 会自动广播事件到你指定的广播驱动器上。</p>
<h3 id="广播名称" tabindex="-1"><a class="header-anchor" href="#广播名称"><span>广播名称</span></a></h3>
<p>Laravel 默认会使用事件的类名作为广播名称来广播事件。不过，你也可以在事件类中通过定义一个 <code v-pre>broadcastAs</code> 方法来自定义广播名称:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 事件的广播名称。</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">string</span></span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">broadcastAs</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token string single-quoted-string">'server.created'</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果您使用 <code v-pre>broadcastAs</code> 方法自定义广播名称，你需要在你使用订阅事件的时候为事件类加上 <code v-pre>.</code> 前缀。这将指示 Echo 不要将应用程序的命名空间添加到事件中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token operator">.</span><span class="token function">listen</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'.server.created'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span>e<span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token operator">...</span><span class="token operator">.</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="广播数据" tabindex="-1"><a class="header-anchor" href="#广播数据"><span>广播数据</span></a></h3>
<p>当一个事件被广播时，它所有的 <code v-pre>public</code> 属性会自动被序列化为广播数据，这允许你在你的 JavaScript 应用中访问事件的公有数据。因此，举个例子，如果你的事件有一个公有的 <code v-pre>$user</code> 属性，它包含了一个 Elouqent 模型，那么事件的广播数据会是：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token string double-quoted-string">"user"</span><span class="token punctuation">:</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token string double-quoted-string">"id"</span><span class="token punctuation">:</span> <span class="token number">1</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string double-quoted-string">"name"</span><span class="token punctuation">:</span> <span class="token string double-quoted-string">"Patrick Stewart"</span></span>
<span class="line">        <span class="token operator">...</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>然而，如果你想更细粒度地控制你的广播数据，你可以添加一个 <code v-pre>broadcastWith</code> 方法到你的事件中。这个方法应该返回一个数组，该数组中的数据会被添加到广播数据中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 指定广播数据</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">array</span></span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">broadcastWith</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'id'</span> <span class="token operator">=></span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">user</span><span class="token operator">-></span><span class="token property">id</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="广播队列" tabindex="-1"><a class="header-anchor" href="#广播队列"><span>广播队列</span></a></h3>
<p>默认情况下，每一个广播事件都被添加到默认的队列上，默认的队列连接在 <code v-pre>queue.php</code> 配置文件中指定。你可以通过在事件类中定义一个 <code v-pre>broadcastQueue</code> 属性来自定义广播器使用的队列。该属性用于指定广播使用的队列名称：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 指定事件被放置在哪个队列上</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@var</span> <span class="token class-name"><span class="token keyword">string</span></span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token variable">$broadcastQueue</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'your-queue-name'</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果要使用 <code v-pre>sync</code> 队列而不是默认队列驱动程序广播你的事件，你可以实现 <code v-pre>ShouldBroadcastNow</code> 接口而不是 <code v-pre>ShouldBroadcast</code>:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Contracts<span class="token punctuation">\</span>Broadcasting<span class="token punctuation">\</span>ShouldBroadcastNow</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">ShippingStatusUpdated</span> <span class="token keyword">implements</span> <span class="token class-name">ShouldBroadcastNow</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">//</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="广播条件" tabindex="-1"><a class="header-anchor" href="#广播条件"><span>广播条件</span></a></h3>
<p>有时，你想在给定条件为 true ，才广播你的事件。你可以通过在事件类中添加一个 <code v-pre>broadcastWhen</code> 方法来定义这些条件：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * Determine if this event should broadcast.</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">bool</span></span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">broadcastWhen</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">value</span> <span class="token operator">></span> <span class="token number">100</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="频道授权-1" tabindex="-1"><a class="header-anchor" href="#频道授权-1"><span>频道授权</span></a></h2>
<p>对于私有频道，用户只有被授权后才能监听。实现过程是用户向你的 Laravel 应用程序发起一个携带频道名称的 HTTP 请求，你的应用程序判断该用户是否能够监听该频道。在使用 <a href="#installing-laravel-echo">Laravel Echo</a> 时，上述 HTTP 请求会被自动发送；尽管如此，你仍然需要定义适当的路由来响应这些请求。</p>
<h3 id="定义授权路由" tabindex="-1"><a class="header-anchor" href="#定义授权路由"><span>定义授权路由</span></a></h3>
<p>幸运的是，我们可以在 Laravel 里很容易地定义路由来响应频道授权请求。在 <code v-pre>BroadcastServiceProvider</code> 中，你会看到一个对 <code v-pre>Broadcast::routes</code> 方法的调用。该方法会注册 <code v-pre>/broadcasting/auth</code> 路由来处理授权请求：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Broadcast</span><span class="token operator">::</span><span class="token function">routes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p><code v-pre>Broadcast::routes</code> 方法会自动把它的路由放进 <code v-pre>web</code> 中间件组中；另外，如果你想对一些属性自定义，可以向该方法传递一个包含路由属性的数组：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Broadcast</span><span class="token operator">::</span><span class="token function">routes</span><span class="token punctuation">(</span><span class="token variable">$attributes</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="定义授权回调" tabindex="-1"><a class="header-anchor" href="#定义授权回调"><span>定义授权回调</span></a></h3>
<p>接下来，我们需要定义真正用于处理频道授权的逻辑。这是在 <code v-pre>routes/channels.php</code> 文件中完成。在该文件中，你可以用 <code v-pre>Broadcast::channel</code> 方法来注册频道授权回调函数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Broadcast</span><span class="token operator">::</span><span class="token function">channel</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'order.{orderId}'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$user</span><span class="token punctuation">,</span> <span class="token variable">$orderId</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$user</span><span class="token operator">-></span><span class="token property">id</span> <span class="token operator">===</span> <span class="token class-name static-context">Order</span><span class="token operator">::</span><span class="token function">findOrNew</span><span class="token punctuation">(</span><span class="token variable">$orderId</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token property">user_id</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>channel</code> 方法接收两个参数：频道名称和一个回调函数，该回调通过返回 <code v-pre>true</code> 或 <code v-pre>false</code> 来表示用户是否被授权监听该频道。</p>
<p>所有的授权回调接收当前被认证的用户作为第一个参数，任何额外的通配符参数作为后续参数。在本例中，我们使用 <code v-pre>{orderId}</code> 占位符来表示频道名称的「ID」部分是通配符。</p>
<h4 id="授权回调模型绑定" tabindex="-1"><a class="header-anchor" href="#授权回调模型绑定"><span>授权回调模型绑定</span></a></h4>
<p>就像 HTTP 路由一样，频道路由也可以利用显式或隐式 <a href="https://learnku.com/docs/laravel/5.5/routing#route-model-binding" target="_blank" rel="noopener noreferrer">路由模型绑定</a>。例如，相比于接收一个字符串或数字类型的 order ID，你也可以请求一个真正的 <code v-pre>Order</code> 模型实例:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Order</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Broadcast</span><span class="token operator">::</span><span class="token function">channel</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'order.{order}'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$user</span><span class="token punctuation">,</span> <span class="token class-name type-declaration">Order</span> <span class="token variable">$order</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$user</span><span class="token operator">-></span><span class="token property">id</span> <span class="token operator">===</span> <span class="token variable">$order</span><span class="token operator">-></span><span class="token property">user_id</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="对事件进行广播" tabindex="-1"><a class="header-anchor" href="#对事件进行广播"><span>对事件进行广播</span></a></h2>
<p>一旦你已经定义好了一个事件并实现了 <code v-pre>ShouldBroadcast</code> 接口，剩下的就是使用 <code v-pre>event</code> 函数来触发该事件。事件分发器会识别出实现了 <code v-pre>ShouldBroadcast</code> 接口的事件并将它们加入到队列进行广播：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">event</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">ShippingStatusUpdated</span><span class="token punctuation">(</span><span class="token variable">$update</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="只广播给他人" tabindex="-1"><a class="header-anchor" href="#只广播给他人"><span>只广播给他人</span></a></h3>
<p>当创建一个使用到事件广播的应用程序时，你可以用 <code v-pre>broadcast</code> 函数来替代 <code v-pre>event</code> 函数。和 <code v-pre>event</code> 函数一样，<code v-pre>broadcast</code> 函数将事件分发到服务端侦听器：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">broadcast</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">ShippingStatusUpdated</span><span class="token punctuation">(</span><span class="token variable">$update</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>不同的是 <code v-pre>broadcast</code> 函数有一个 <code v-pre>toOthers</code> 方法允许你将当前用户从广播接收者中排除：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">broadcast</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">ShippingStatusUpdated</span><span class="token punctuation">(</span><span class="token variable">$update</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">toOthers</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>为了更好地理解什么时候使用 <code v-pre>toOthers</code> 方法，让我们假设有一个任务列表的应用程序，用户可以通过输入任务名称来新建任务。为了新建任务，你的应用程序需要发起一个请求到 <code v-pre>/task</code> 路由，该路由在接收到请求并成功创建新任务后会触发一个任务被新建的事件广播，并返回新任务的 JSON 响应。当你的 JavaScript 应用程序接收到来自该路由的响应时，它会直接将新任务插入到任务列表，就像这样：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">axios<span class="token operator">.</span><span class="token function">post</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/task'</span><span class="token punctuation">,</span> task<span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">.</span><span class="token function">then</span><span class="token punctuation">(</span><span class="token punctuation">(</span>response<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span></span>
<span class="line">        this<span class="token operator">.</span>tasks<span class="token operator">.</span><span class="token function">push</span><span class="token punctuation">(</span>response<span class="token operator">.</span>data<span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>然而，别忘了我们还将接收到一个事件广播。如果你的 JavaScript 应用程序同时监听该事件以便添加新任务到任务列表，你将会在你的列表中看到重复的任务：一份来自路由响应，另一份来自广播。</p>
<p>你可以通过使用 <code v-pre>toOthers</code> 方法让广播器只广播事件到其他用户来解决该问题。</p>
<h4 id="配置-1" tabindex="-1"><a class="header-anchor" href="#配置-1"><span>配置</span></a></h4>
<p>当你实例化 Laravel Echo 实例时，一个套接字 ID 会被指定到该连接。如果你使用 <a href="https://vuejs.org/" target="_blank" rel="noopener noreferrer">Vue</a> 和 <a href="https://github.com/mzabriskie/axios" target="_blank" rel="noopener noreferrer">Axios</a>，套接字 ID 会自动被添加到每一个请求的 <code v-pre>X-Socket-ID</code> 头中。然后，当你调用 <code v-pre>toOthers</code> 方法时，Laravel 会从头中提取出套接字 ID，并告诉广播器不要广播任何消息到该套接字 ID 的连接上。</p>
<p>如果你没有使用 Vue 和 Axios，则需要手动配置 JavaScript 应用程序来发送 <code v-pre>X-Socket-ID</code> 头。你可以用 <code v-pre>Echo.socketId</code> 方法来获取套接字 ID：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">var</span> socketId <span class="token operator">=</span> <span class="token keyword">Echo</span><span class="token operator">.</span><span class="token function">socketId</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h2 id="接收广播" tabindex="-1"><a class="header-anchor" href="#接收广播"><span>接收广播</span></a></h2>
<h3 id="安装-laravel-echo" tabindex="-1"><a class="header-anchor" href="#安装-laravel-echo"><span>安装 Laravel Echo</span></a></h3>
<p>Laravel Echo 是一个 JavaScript 库，它使得订阅频道和监听由 Laravel 广播的事件变得非常容易。你可以通过 NPM 包管理器来安装 Echo。在本例中，因为我们将使用 Pusher 广播器，请安装 <code v-pre>pusher-js</code> 包：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">npm install <span class="token operator">--</span>save laravel<span class="token operator">-</span><span class="token keyword">echo</span> pusher<span class="token operator">-</span>js</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>一旦 Echo 被安装好，你就可以在你应用程序的 JavaScript 中创建一个全新的 Echo 实例。做这件事的一个理想地方是在 <code v-pre>resources/assets/js/bootstrap.js</code> 文件的底部，Laravel 框架自带了该文件：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">import <span class="token keyword">Echo</span> from <span class="token string double-quoted-string">"laravel-echo"</span></span>
<span class="line"></span>
<span class="line">window<span class="token operator">.</span><span class="token keyword">Echo</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token keyword">Echo</span><span class="token punctuation">(</span><span class="token punctuation">{</span></span>
<span class="line">    broadcaster<span class="token punctuation">:</span> <span class="token string single-quoted-string">'pusher'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">key</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'your-pusher-key'</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>当你使用 <code v-pre>pusher</code> 连接器来创建一个 Echo 实例的时候，你需要指定 <code v-pre>cluster</code> 以及指定连接是否需要加密：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">window<span class="token operator">.</span><span class="token keyword">Echo</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token keyword">Echo</span><span class="token punctuation">(</span><span class="token punctuation">{</span></span>
<span class="line">    broadcaster<span class="token punctuation">:</span> <span class="token string single-quoted-string">'pusher'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">key</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'your-pusher-key'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">cluster</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'eu'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">encrypted</span><span class="token punctuation">:</span> <span class="token constant boolean">true</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="对事件进行监听" tabindex="-1"><a class="header-anchor" href="#对事件进行监听"><span>对事件进行监听</span></a></h3>
<p>一旦你安装好并实例化了 Echo，你就可以开始监听事件广播了。首先，使用 <code v-pre>channel</code> 方法来获取一个频道实例，然后调用 <code v-pre>listen</code> 方法来监听指定的事件：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">Echo</span><span class="token operator">.</span><span class="token function">channel</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'orders'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">.</span><span class="token function">listen</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'OrderShipped'</span><span class="token punctuation">,</span> <span class="token punctuation">(</span>e<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span></span>
<span class="line">        console<span class="token operator">.</span><span class="token function">log</span><span class="token punctuation">(</span>e<span class="token operator">.</span>order<span class="token operator">.</span>name<span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果你想监听私有频道上的事件，请使用 <code v-pre>private</code> 方法。你可以通过链式调用 <code v-pre>listen</code> 方法来监听一个频道上的多个事件：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">Echo</span><span class="token operator">.</span><span class="token keyword">private</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'orders'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">.</span><span class="token function">listen</span><span class="token punctuation">(</span><span class="token operator">...</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">.</span><span class="token function">listen</span><span class="token punctuation">(</span><span class="token operator">...</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">.</span><span class="token function">listen</span><span class="token punctuation">(</span><span class="token operator">...</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="退出频道" tabindex="-1"><a class="header-anchor" href="#退出频道"><span>退出频道</span></a></h3>
<p>如果想退出频道，你需要在你的 Echo 实例上调用 <code v-pre>leave</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">Echo</span><span class="token operator">.</span><span class="token function">leave</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'orders'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="命名空间" tabindex="-1"><a class="header-anchor" href="#命名空间"><span>命名空间</span></a></h3>
<p>你可能注意到了在上面的例子中我们没有为事件类指定完整的命名空间。这是因为 Echo 会自动认为事件在 <code v-pre>App\Events</code> 命名空间下。你可以在实例化 Echo 的时候传递一个 <code v-pre>namespace</code> 配置选项来指定根命名空间：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">window<span class="token operator">.</span><span class="token keyword">Echo</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token keyword">Echo</span><span class="token punctuation">(</span><span class="token punctuation">{</span></span>
<span class="line">    broadcaster<span class="token punctuation">:</span> <span class="token string single-quoted-string">'pusher'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">key</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'your-pusher-key'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token keyword">namespace</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'App.Other.Namespace'</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>另外，你也可以在使用 Echo 订阅事件的时候为事件类加上 <code v-pre>.</code> 前缀。这时需要填写完全限定名称的类名：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">Echo</span><span class="token operator">.</span><span class="token function">channel</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'orders'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">.</span><span class="token function">listen</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'.Namespace.Event.Class'</span><span class="token punctuation">,</span> <span class="token punctuation">(</span>e<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token comment">//</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="presence-频道" tabindex="-1"><a class="header-anchor" href="#presence-频道"><span>Presence 频道</span></a></h2>
<p>Presence 频道是在私有频道的安全性基础上，额外暴露出有哪些人订阅了该频道。这使得它可以很容易地建立强大的、协同的应用，如当有一个用户在浏览页面时，通知其他正在浏览相同页面的用户。</p>
<h3 id="授权-presence-频道" tabindex="-1"><a class="header-anchor" href="#授权-presence-频道"><span>授权 Presence 频道</span></a></h3>
<p>Presence 频道也是私有频道；因此，用户必须 <a href="#authorizing-channels">获取授权后才能访问他们</a>。与私有频道不同的是，在给 presence 频道定义授权回调函数时，如果一个用户已经加入了该频道，那么不应该返回 <code v-pre>true</code>，而应该返回一个关于该用户信息的数组。</p>
<p>由授权回调函数返回的数据能够在你的 JavaScript 应用程序中被 presence 频道事件侦听器所使用。如果用户没有获得加入该 presence 频道的授权，那么你应该返回 <code v-pre>false</code> 或 <code v-pre>null</code>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Broadcast</span><span class="token operator">::</span><span class="token function">channel</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'chat.{roomId}'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$user</span><span class="token punctuation">,</span> <span class="token variable">$roomId</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$user</span><span class="token operator">-></span><span class="token function">canJoinRoom</span><span class="token punctuation">(</span><span class="token variable">$roomId</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'id'</span> <span class="token operator">=></span> <span class="token variable">$user</span><span class="token operator">-></span><span class="token property">id</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token variable">$user</span><span class="token operator">-></span><span class="token property">name</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="加入-presence-频道" tabindex="-1"><a class="header-anchor" href="#加入-presence-频道"><span>加入 Presence 频道</span></a></h3>
<p>你可以用 Echo 的 <code v-pre>join</code> 方法来加入 presence 频道。<code v-pre>join</code> 方法会返回一个实现了 <code v-pre>PresenceChannel</code> 的对象，它通过暴露 <code v-pre>listen</code> 方法，允许你订阅 <code v-pre>here</code>、<code v-pre>joining</code> 和 <code v-pre>leaving</code> 事件。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">Echo</span><span class="token operator">.</span><span class="token function">join</span><span class="token punctuation">(</span><span class="token string backtick-quoted-string">`chat.${roomId}`</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">.</span><span class="token function">here</span><span class="token punctuation">(</span><span class="token punctuation">(</span>users<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token comment">//</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">.</span><span class="token function">joining</span><span class="token punctuation">(</span><span class="token punctuation">(</span>user<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span></span>
<span class="line">        console<span class="token operator">.</span><span class="token function">log</span><span class="token punctuation">(</span>user<span class="token operator">.</span>name<span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">.</span><span class="token function">leaving</span><span class="token punctuation">(</span><span class="token punctuation">(</span>user<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span></span>
<span class="line">        console<span class="token operator">.</span><span class="token function">log</span><span class="token punctuation">(</span>user<span class="token operator">.</span>name<span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>here</code> 回调函数会在你成功加入频道后被立即执行，它接收一个包含用户信息的数组，用来告知当前订阅在该频道上的其他用户。<code v-pre>joining</code> 方法会在其他新用户加入到频道时被执行，<code v-pre>leaving</code> 会在其他用户退出频道时被执行。</p>
<h3 id="广播到-presence-频道" tabindex="-1"><a class="header-anchor" href="#广播到-presence-频道"><span>广播到 Presence 频道</span></a></h3>
<p>Presence 频道可以像公开和私有频道一样接收事件。使用一个聊天室的例子，我们要把 <code v-pre>NewMessage</code> 事件广播到聊天室的 presence 频道。要实现它，我们将从事件的 <code v-pre>broadcastOn</code> 方法中返回一个 <code v-pre>PresenceChannel</code> 实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 指定事件在哪些频道上进行广播</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name">Channel<span class="token punctuation">|</span><span class="token keyword">array</span></span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">broadcastOn</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">PresenceChannel</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'room.'</span><span class="token operator">.</span><span class="token variable">$this</span><span class="token operator">-></span><span class="token property">message</span><span class="token operator">-></span><span class="token property">room_id</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>和公开或私有事件一样，presence 频道事件也能使用 <code v-pre>broadcast</code> 函数来广播。同样的，你还能用 <code v-pre>toOthers</code> 方法将当前用户从广播接收者中排除：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">broadcast</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">NewMessage</span><span class="token punctuation">(</span><span class="token variable">$message</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token function">broadcast</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">NewMessage</span><span class="token punctuation">(</span><span class="token variable">$message</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">toOthers</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你可以通过 Echo 的 <code v-pre>listen</code> 方法来监听 join 事件：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">Echo</span><span class="token operator">.</span><span class="token function">join</span><span class="token punctuation">(</span><span class="token string backtick-quoted-string">`chat.${roomId}`</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">.</span><span class="token function">here</span><span class="token punctuation">(</span><span class="token operator">...</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">.</span><span class="token function">joining</span><span class="token punctuation">(</span><span class="token operator">...</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">.</span><span class="token function">leaving</span><span class="token punctuation">(</span><span class="token operator">...</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">.</span><span class="token function">listen</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'NewMessage'</span><span class="token punctuation">,</span> <span class="token punctuation">(</span>e<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token comment">//</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="客户端事件" tabindex="-1"><a class="header-anchor" href="#客户端事件"><span>客户端事件</span></a></h2>
<p>有时候你可能希望广播一个事件给其他已经连接的客户端，但并不需要通知你的 Laravel 程序。试想一下当你想提醒用户，另外一个用户正在输入中的时候，显而易见客服端事件对于「输入中」之类的通知就显得非常有用了。你可以使用 Echo 的 <code v-pre>whisper</code> 方法来广播客户端事件：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">Echo</span><span class="token operator">.</span><span class="token function">channel</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'chat'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">.</span><span class="token function">whisper</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'typing'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span></span>
<span class="line">        name<span class="token punctuation">:</span> this<span class="token operator">.</span>user<span class="token operator">.</span>name</span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你可以使用 <code v-pre>listenForWhisper</code> 方法来监听客户端事件：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">Echo</span><span class="token operator">.</span><span class="token function">channel</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'chat'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">.</span><span class="token function">listenForWhisper</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'typing'</span><span class="token punctuation">,</span> <span class="token punctuation">(</span>e<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span></span>
<span class="line">        console<span class="token operator">.</span><span class="token function">log</span><span class="token punctuation">(</span>e<span class="token operator">.</span>name<span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="消息通知" tabindex="-1"><a class="header-anchor" href="#消息通知"><span>消息通知</span></a></h2>
<p>将 <a href="https://learnku.com/docs/laravel/5.5/notifications" target="_blank" rel="noopener noreferrer">消息通知</a> 和事件广播一同使用，你的 JavaScript 应用程序可以在不刷新页面的情况下接收新的消息通知。首先，请先阅读关于如何使用 <a href="https://learnku.com/docs/laravel/5.5/notifications#broadcast-notifications" target="_blank" rel="noopener noreferrer">the broadcast notification channel</a> 的文档。</p>
<p>一旦你将一个消息通知配置为使用广播频道，你需要使用 Echo 的 <code v-pre>notification</code> 方法来监听广播事件。谨记，频道名称应该和接收消息通知的实体类名相匹配：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">Echo</span><span class="token operator">.</span><span class="token keyword">private</span><span class="token punctuation">(</span><span class="token string backtick-quoted-string">`App.User.${userId}`</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">.</span><span class="token function">notification</span><span class="token punctuation">(</span><span class="token punctuation">(</span>notification<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span></span>
<span class="line">        console<span class="token operator">.</span><span class="token function">log</span><span class="token punctuation">(</span>notification<span class="token operator">.</span>type<span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>在本例中，所有通过 <code v-pre>broadcast</code> 频道发送到 <code v-pre>App\User</code> 实例的消息通知都会被该回调接收到。一个针对 <code v-pre>App.User.{id}</code> 频道的授权回调函数已经被包含在 <code v-pre>Laravel</code> 的 <code v-pre>BroadcastServiceProvider</code> 中了。</p>
<h2 id="译者署名" tabindex="-1"><a class="header-anchor" href="#译者署名"><span>译者署名</span></a></h2>
<table>
<thead>
<tr>
<th>用户名</th>
<th>头像</th>
<th>职能</th>
<th>签名</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="https://learnku.com/users/13655" target="_blank" rel="noopener noreferrer">@沈益飞</a></td>
<td><img src="https://cdn.learnku.com/uploads/avatars/13655_1490162781.png?imageView2/1/w/100/h/100" alt="100"></td>
<td>翻译</td>
<td><a href="https://github.com/m809745357" target="_blank" rel="noopener noreferrer">@m809745357</a> at Github</td>
</tr>
</tbody>
</table>
<hr>
<blockquote>
<p>{note} 欢迎任何形式的转载，但请务必注明出处，尊重他人劳动共创开源社区。</p>
<p>转载请注明：本文档由 Laravel China 社区 <a href="https://laravel-china.org/" target="_blank" rel="noopener noreferrer">laravel-china.org</a> 组织翻译，详见 <a href="https://learnku.com/laravel/t/5756/laravel-55-document-translation-call-come-and-join-the-translation" target="_blank" rel="noopener noreferrer">翻译召集帖</a>。</p>
<p>文档永久地址： <a href="https://learnku.com/docs/laravel" target="_blank" rel="noopener noreferrer">《Laravel 中文文档》</a></p>
</blockquote>
</div></template>


